//
//  ProfileVM.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/6/29.
//

import RxSwift
import Combine
import ToastUI
import Moya
class profileViewModel: ObservableObject {
    @Published var isLoading = false
    @Published var upError: String?

    @Published var isuploadSuccess = false
    @Published var ispostSuccess = false
    @Published var isLikeSuccess = false
    @Published var isUnlikeSuccess = false

    @Published var userinfo:UserProfileResponse?
    @Published var dynamicList: [DynamicItem] = []

    private let disposeBag = DisposeBag()
    init(){
//        profileInfo()
    }
    
    
    func profileInfo () {
//        NetworkManager.shared.userprofile()
//            .observe(on: MainScheduler.instance)
//            .subscribe(
//                onSuccess: { [weak self] response in
//                    self?.isLoading = false
//                    print(response);
//                    self?.userinfo = response
//                },
//                onFailure: { [weak self] error in
//                    self?.handleError(error)
//                    print(error);
//                }
//            )
//            .disposed(by: disposeBag)
        NetworkManager.shared.request(MultiTarget(ApiService.userProfile), responseType: UserProfileResponse.self)
            .observe(on: MainScheduler.instance)
            .subscribe(
                onNext: { [weak self] response in
                    self?.isLoading = false
                    print(response);
                    self?.userinfo = response
                },
                onError: { [weak self] error in
                    self?.handleError(error)
                    print(error);
                }
            )
            .disposed(by: disposeBag)
        
    }
    
    func  uploadImageAvatar (avatarUrl:String) {
//        NetworkManager.shared.uploadAvatar(avatarUrl: avatarUrl)
//            .observe(on: MainScheduler.instance)
//            .subscribe(
//                onSuccess: { [weak self] response in
//                    self?.isLoading = false
////                    self?.upError = "上传成功"
//                    print(response);
//                    self?.isuploadSuccess = true
//                },
//                onFailure: { [weak self] error in
//                    self?.handleError(error)
//                    self?.isuploadSuccess = false
//                    print(error);
//                }
//            )
//            .disposed(by: disposeBag)
        
        
        NetworkManager.shared.request(MultiTarget(ApiService.profileAvatar(avatarUrl: avatarUrl)), responseType: AvatarResponse.self)
            .observe(on: MainScheduler.instance)
            .subscribe(
                onNext: { [weak self] response in
                    self?.isLoading = false
                    print(response);
                    self?.isuploadSuccess = true
                },
                onError: { [weak self] error in
                    self?.handleError(error)
                    self?.isuploadSuccess = false
                    print(error);
                }
            )
            .disposed(by: disposeBag)
    }
    
    
    func postDynamic (userId: Int, content: String, address: String, mediaUrls: [String], createTime: Int) {
//        NetworkManager.shared.dynamicAdd(userId: userId, content: content, address: address, mediaUrls: mediaUrls, createTime: createTime)
//            .observe(on: MainScheduler.instance)
//            .subscribe(
//                onSuccess: { [weak self] response in
//                    self?.isLoading = false
//                    self?.upError = "发布成功"
//                    print(response);
//                    self?.ispostSuccess = true
//                },
//                onFailure: { [weak self] error in
//                    self?.handleError(error)
//                    self?.ispostSuccess = false
//                    print(error);
//                }
//            )
//            .disposed(by: disposeBag)
        NetworkManager.shared.request(MultiTarget(ApiService.dynamicAdd(userId: userId, content: content, address: address, mediaUrls: mediaUrls, createTime: createTime)), responseType: SimpleResponse.self)
            .observe(on: MainScheduler.instance)
            .subscribe(
                onNext: { [weak self] response in
                    self?.isLoading = false
                    self?.upError = "发布成功"
                    print(response);
                    self?.ispostSuccess = true
                },
                onError: { [weak self] error in
                    self?.handleError(error)
                    self?.ispostSuccess = false
                    print(error);
                }
            )
            .disposed(by: disposeBag)
    }
    
    func getDynamicListData(){
//        NetworkManager.shared.getDynamicList()
//            .observe(on: MainScheduler.instance)
//            .subscribe(
//                onSuccess: { [weak self] response in
//                    self?.isLoading = false
//                    print(response);
//                    if let list = response.data {
//                        self?.dynamicList = list
//                    }
//                },
//                onFailure: { [weak self] error in
//                    self?.isLoading = false
//                    self?.upError = error.localizedDescription
//                    self?.ispostSuccess = false
//                    print(error);
//                }
//            )
//            .disposed(by: disposeBag)
        
        
        NetworkManager.shared.request(MultiTarget(ApiService.dynamicList), responseType: SportMomentResponse.self)
            .observe(on: MainScheduler.instance)
            .subscribe(
                onNext: { [weak self] response in
                    self?.isLoading = false
                    self?.dynamicList = response.data ?? []
                    print(response);
                },
                onError: { [weak self] error in
                    self?.handleError(error)
                    self?.ispostSuccess = false
                    print(error);
                }
            )
            .disposed(by: disposeBag)
    }
    
    
    func dynamicLike(dynamicId: Int) {
//        NetworkManager.shared.dynamicLike(dynamicId: dynamicId)
//            .observe(on: MainScheduler.instance)
//            .subscribe(
//                onSuccess: { [weak self] response in
//                    self?.isLoading = false
//                    self?.isLikeSuccess = true
//                    print(response);
//                },
//                onFailure: { [weak self] error in
//                    self?.handleError(error)
//                    self?.isLikeSuccess = false
//                    print(error);
//                }
//            )
//            .disposed(by: disposeBag)
        NetworkManager.shared.request(MultiTarget(ApiService.dynamicLike(dynamicId: dynamicId)), responseType: SimpleResponse.self)
            .observe(on: MainScheduler.instance)
            .subscribe(
                onNext: { [weak self] response in
                    self?.isLoading = false
                    self?.isLikeSuccess = true
                    print(response);
                },
                onError: { [weak self] error in
                    self?.handleError(error)
                    self?.isLikeSuccess = false
                    print(error);
                }
            )
            .disposed(by: disposeBag)
    }
    
    
    func dynamicUnlike(dynamicId: Int) {
//        NetworkManager.shared.dynamicUnlike(dynamicId: dynamicId)
//            .observe(on: MainScheduler.instance)
//            .subscribe(
//                onSuccess: { [weak self] response in
//                    self?.isLoading = false
//                    self?.isUnlikeSuccess = true
//                    print(response);
//                },
//                onFailure: { [weak self] error in
//                    self?.handleError(error)
//                    self?.isUnlikeSuccess = false
//                    print(error);
//                }
//            )
//            .disposed(by: disposeBag)
        NetworkManager.shared.request(MultiTarget(ApiService.dynamicUnlike(dynamicId: dynamicId)), responseType: SimpleResponse.self)
            .observe(on: MainScheduler.instance)
            .subscribe(
                onNext: { [weak self] response in
                    self?.isLoading = false
                    self?.isUnlikeSuccess = true
                    print(response);
                },
                onError: { [weak self] error in
                    self?.handleError(error)
                    self?.isUnlikeSuccess = false
                    print(error);
                }
            )
            .disposed(by: disposeBag)
    }
    
    private func handleError(_ error: Error) {
        self.isLoading = false
        if let networkError = error as? NetworkError {
            upError = networkError.errorDescription
//            if networkError.isAuthenticationError {
//                AppState.shared.logout()
//            }
        } else {
            upError = error.localizedDescription
        }
    }
    
}
